#Author: Tanyong
# *-* coding:utf8 *-*
# @time       : 2018/7/18 22:28
# @Author     : Tanyong
# @Site       : 
# @File       : views.py
# @Software   : PyCharm
from flask import flash
from flask import redirect
from flask import render_template
from flask import url_for
from wtforms import StringField
from wtforms.validators import DataRequired

from info import db
from info.models import Info
from . import admin_blu
from .forms import InfoForm

@admin_blu.route('/admin')
def admin():
    data_info = Info.query.all()
    return render_template('admin/center.html', alldatainfo=data_info)

@admin_blu.route("/admin/add_info", methods=['GET', 'POST'])
def add_info():
    """添加股票信息"""
    form = InfoForm()
    if form.validate_on_submit():
        data = form.data
        infoadd = Info(
            code=data['code'],
            short=data['short'],
            chg=data['chg'],
            turnover=data['turnover'],
            price=data['price'],
            highs=data['highs'],
        )
        db.session.add(infoadd)
        db.session.commit()
        flash("添加股票信息成功！", "ok")
        return redirect(url_for('admin.admin'))
    return render_template('admin/infoAdd.html', form=form)

@admin_blu.route('/admin/del/<code>')
def delete_stock(code):
    # 1, 先查询 股票列表中是否有这只股票
    stock = Info.query.filter(Info.code==code).first()
    if not stock:
        # 如果根本没有这只股票,则无法删除
        flash("压根儿没有这只股票, 无法删除!")
    else:
        # 查询到股票, 才能执行删除操作
        try:
            db.session.delete(stock)
            db.session.commit()
        except Exception as e:
            db.session.rollback()
            print(e)
            flash('删除股票失败!')

    return redirect(url_for('admin.admin'))


@admin_blu.route('/admin/update/<code>', methods=['GET','POST'])
def update_info(code):
    # 1, 先查询 股票列表中是否有这只股票
    stock = Info.query.filter(Info.code==code).first()

    class Update_info_Form(InfoForm):
        code = StringField("股票代码", validators=[DataRequired("请输入股票代码")], default=stock.code)
        short = StringField("股票简称", validators=[DataRequired('请输入股票简称')], default=stock.short)
        chg = StringField("涨跌幅", validators=[DataRequired("请输入涨跌幅")], default=stock.chg)
        turnover = StringField("股票换手率", validators=[DataRequired("请输入股票换手率")], default=stock.turnover)
        price = StringField("股票价格", validators=[DataRequired("请输入股票价格")], default=stock.price)
        highs = StringField("股票最高点", validators=[DataRequired("请输入股票最高点")], default=stock.highs)
    form = Update_info_Form()

    if not stock:
        flash("压根儿没有这只股票, 无法修改!")
    else:
        # 如果与查询到该股票, 才能进备注信息修改
        if form.validate_on_submit():
            # 如果是有效数据提交, 则进行更新备注信息逻辑
            data = form.data
            stock.code=data['code']
            stock.short=data['short']
            stock.chg=data['chg']
            stock.turnover=data['turnover']
            stock.price=data['price']
            stock.highs=data['highs']
            try:
                db.session.commit()
            except Exception as e:
                db.session.rollback()
                print(e)
                flash('修改股票信息失败')
            return redirect(url_for('admin.admin'))

    return render_template('admin/infoUpdate.html', form=form)

